package com.whut.whw.personalinfo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.whut.whw.R;
import com.whut.whw.base.BaseFragment;
import com.whut.whw.model.User;
import com.whut.whw.personalinfo.helpandfeedback.HelpAndFeedbackActivity;
import com.whut.whw.personalinfo.mythings.MyThingsActivity;
import com.whut.whw.personalinfo.mywant.MyWantActivity;
import com.whut.whw.setting.SettingsActivity;
import com.whut.whw.util.GsonUtils;
import com.whut.whw.util.LogUtils;
import com.whut.whw.util.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

import static android.app.Activity.RESULT_OK;
import static com.whut.whw.R.id.head_account;
import static com.whut.whw.R.id.head_nickname;
import static com.whut.whw.R.id.rv_psnlist;
import static com.whut.whw.constant.ConfigConsts.BASE_URL;
import static io.rong.imlib.statistics.Statistics.TAG;

/**
 * Created by 曾旺红 on 2017/06/08.
 */

public class PersonalInfoFragment extends BaseFragment {

    private List<Object> psnlist = new ArrayList<>();

    public static final int REQUEST_INFO = 1;

    private User user;

    public static PersonalInfoFragment newInstance() {
        return new PersonalInfoFragment();
    }

    RecyclerView recyclerView;
    PersonalInfoAdapter adapter;

    private void initPerson() {
        uploadPost();
    }


    @Override
    public View initView(LayoutInflater inflater) {
        View rootview = inflater.inflate(R.layout.fragment_personal, null);
        recyclerView = (RecyclerView) rootview.findViewById(rv_psnlist);
        return rootview;
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        initPerson();
        //指定布局方式
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        //适配器设置
        adapter = new PersonalInfoAdapter(psnlist);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void setListener() {
        adapter.setmOnItemClickListener(new PersonalInfoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (position == 0) {
                    LinearLayout headBack = (LinearLayout) v.findViewById(R.id.psn_data);
                    headBack.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getContext(), PsnDataActivity.class);
                            intent.putExtra("ID",user.getYhbh());
                            startActivityForResult(intent, REQUEST_INFO);
                        }
                    });

                    TextView follow = (TextView) v.findViewById(R.id.head_follow);
                    follow.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getContext(), FollowListActivity.class);
                            startActivity(intent);
                        }
                    });

                    TextView fan = (TextView) v.findViewById(R.id.head_fan);
                    fan.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getContext(), FollowListActivity.class);
                            startActivity(intent);
                        }
                    });
                } else {
                    TextView item_name_tv = (TextView) v.findViewById(R.id.item_name);
                    String item_name = item_name_tv.getText().toString();
                    if ("我的物品".equals(item_name)) {
                        Intent intent = new Intent(context, MyThingsActivity.class);
                        startActivity(intent);
                    } else if ("我想要的".equals(item_name)) {
                        Intent intent = new Intent(context, MyWantActivity.class);
                        startActivity(intent);
                    } else if ("我的收藏".equals(item_name)) {

                    } else if ("我的集市".equals(item_name)) {

                    } else if ("帮助反馈".equals(item_name)) {
                        Intent intent = new Intent(context, HelpAndFeedbackActivity.class);
                        startActivity(intent);
                    } else if ("设置".equals(item_name)) {
                        Intent intent = new Intent(context, SettingsActivity.class);
                        startActivity(intent);
                    }
                    getUiDelegate().toastShort(item_name);
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_INFO) {
                String nickname = data.getStringExtra("昵称");
                String location = data.getStringExtra("城市");

                TextView nname = (TextView) getActivity().findViewById(head_nickname);
                TextView lct = (TextView) getActivity().findViewById(head_account);

                nname.setText(nickname);
                lct.setText(location);
            }
        }
    }
    /**
     * post
     */
    public void uploadPost() {
        final String TAG="PersonalInfoFragment:";
        Map<String, String> params = new HashMap<>();
        params.put("id", "8a0acbc75d11e6cb015d11e70bde0000");

        String url = BASE_URL + "personal/maininfo";
        OkHttpUtils.post()//
                .url(url)
                .params(params)//
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        LogUtils.d(TAG, "error");
                        ToastUtils.showLong("error: "+e.toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogUtils.d(TAG, response);
                        //ToastUtils.showLong(response);
                        jsonToString(response);
                    }
                });
    }

    /**
     * 解析JSon字符串
     * @param json
     */
    public void jsonToString(String json){
        PersonalJSon pj = GsonUtils.GsonToBean(json,PersonalJSon.class);

        //显示昵称，地址，信誉度，关注量，粉丝量
        String nc=pj.getData().getUser().getNc()==null?"设置昵称":pj.getData().getUser().getNc();
        String dz=pj.getData().getUser().getYhdz()==null?"设置城市":pj.getData().getUser().getYhdz();
        int xyd=pj.getData().getUser().getXyd();
        String gz="关注 "+pj.getData().getAttenNum();
        String fs="粉丝 "+pj.getData().getFanNum();
        String yhtx=BASE_URL+"upload_img/tu2.jpg";
        //user.setYhtx(yhtx);
        user=pj.getData().getUser();
        String photopath="http://upload.jianshu.io/users/upload_avatars/3358298/167277b0-84f6-4b2c-a740-d117ab4687b6.jpg";
        psnlist.add(new Object[]{
                yhtx,
                nc,
                dz,
                xyd,
                gz,
                fs
        });
        PersonalInfoModel info1 = new PersonalInfoModel(R.drawable.ic_friends_24dp, "我的物品", R.drawable.ic_arrow_right_24dp);
        psnlist.add(info1);
        PersonalInfoModel info2 = new PersonalInfoModel(R.drawable.ic_friends_24dp, "我想要的", R.drawable.ic_arrow_right_24dp);
        psnlist.add(info2);
        PersonalInfoModel info3 = new PersonalInfoModel(R.drawable.ic_friends_24dp, "我的收藏", R.drawable.ic_arrow_right_24dp);
        psnlist.add(info3);
        PersonalInfoModel info4 = new PersonalInfoModel(R.drawable.ic_friends_24dp, "我的集市", R.drawable.ic_arrow_right_24dp);
        psnlist.add(info4);
        PersonalInfoModel info5 = new PersonalInfoModel(R.drawable.ic_friends_24dp, "帮助反馈", R.drawable.ic_arrow_right_24dp);
        psnlist.add(info5);
        PersonalInfoModel info6 = new PersonalInfoModel(R.drawable.ic_friends_24dp, "设置", R.drawable.ic_arrow_right_24dp);
        psnlist.add(info6);
        adapter.notifyDataSetChanged();

    }
}
